package com.example.demo.controller;

import com.example.demo.controller.data.ReturnData;
import com.example.demo.controller.vo.RegisterVo;
import com.example.demo.dao.UserDao;
import com.example.demo.entity.User;
import com.example.demo.sevice.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
public class UserController {
//    @Resource


    @Autowired
    UserService userService;
    //获取当前登录用户信息
    @GetMapping("/getUser")
    ReturnData getUser( HttpServletRequest request){
        String token = request.getHeader("token");
        return  userService.getUser(token);
    }

    //登陆接口
    @PostMapping("login")
    public ReturnData login(@RequestBody RegisterVo user) {
        return  userService.login(user.getUsername(),user.getPassword());
    }
//    {
//            "username": "lan"
//        ,
//            "password": "123"
//        ,
//            "nickname": "aaa"
//        ,
//            "avtar": "sadsa"
//        ,
//            "sex": "2"
//        ,
//            "age": "12"
//        ,
//            "phone": "1817601554"
//    }
    @PostMapping("register")
    public ReturnData register(@RequestBody RegisterVo user) {
        return  userService.register(user);
    }
    //获取验证码接口
    @PostMapping("getCode")
    public ReturnData getCode(String email) throws InterruptedException {
        return  userService.getCode(email);
    }

    /**
     * 更新用户信息
     * @param user 用来接收需要更新的字段
     * @return
     * @throws InterruptedException
     */
    @PostMapping("updateUserInfo")
    public ReturnData updateUserInfo(@RequestBody RegisterVo user, HttpServletRequest request)  {
        String token = request.getHeader("token");
        return  userService.updateUserInfo(user,token);
    }

}
